package com.cos.shop.web.filter; /**
 * @author: COS
 * @time: 2023/5/9 11:25
 * @description:
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebFilter(filterName = "Filter01_Charset",value = "/*")
public class Filter01_Charset implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req= (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        if(check(req.getRequestURI())){
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
        }
        chain.doFilter(req, resp);
    }

    public boolean check(String path){
        List<String> list = Arrays.asList("/", ".html", ".css", ".js", ".png", ".jpg");
        for (String s : list) {
            if (path.endsWith(s)) {
                return false;
            }
        }
        return true;
    }

}
